package com.example.yy.helloword0001;


import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MyDrawerActivity extends AppCompatActivity  implements View.OnClickListener{

    private DrawerLayout drawer_layout;

    private ListView lv;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_drawer);

        //初始化 ListView
        lv = findViewById(R.id.list_left_drawer);

        //初始化数据源
        List<String> list  = new ArrayList<>();
        for (int i=0;i<50;i++){
            list.add("数据"+i);
        }
        //适配初始化
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        lv.setAdapter(adapter);

        //listView item单击事件

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //1.获取listview 里的值
                TextView textView = view.findViewById(android.R.id.text1);
                String _id = textView.getText().toString();
                Toast.makeText(MyDrawerActivity.this,"heheh"+_id,Toast.LENGTH_LONG).show();

                if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
                    drawer_layout.closeDrawer(GravityCompat.START);
                }

                //2.获取listview 里的值,数据源用map 的情况, 数据源什么类型就用什么类型
                //Map item = (Map)parent.getItemAtPosition(position);
                //Toast.makeText(MainActivity.this,"heheh"+item.get("_id"),Toast.LENGTH_LONG).show();
                //Toast.makeText(MainActivity.this,"hehhah"+id,Toast.LENGTH_LONG).show();
            }
        });


        drawer_layout =  findViewById(R.id.drawer_layout);
        Button open_button = findViewById(R.id.open_drawer);
        open_button.setOnClickListener(this);

        //默认隐藏侧边栏
        drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
                Gravity.START);

    }

    //单击按钮，显示侧边栏
    @Override
    public void onClick(View v) {
        drawer_layout.openDrawer(Gravity.START);
        drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
                Gravity.START);    //解除锁定
    }

//    点击侧边栏以外，隐藏侧边栏
    @Override
    public void onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}
